// pages/orders/index/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search: "",

    pageNum: 1,
    pageSize: 10,
    active: 1,
    orderType: [{
        id: 1,
        name: "待支付"
      },
      {
        id: 2,
        name: "待收货"
      },
      {
        id: 3,
        name: "已完成"
      },
      {
        id: 4,
        name: "我的评价"
      },
      {
        id: 5,
        name: "退款/售后"
      }
    ],
    list: []
  },
  //去评价
  toComment(e) {
    let orderId = e.currentTarget.dataset.item.orderId
    let pkIds = JSON.stringify( e.currentTarget.dataset.item.pkIds)
    wx.navigateTo({
      url: `/pages/orders/evaluation/index?id=${orderId}&px=${pkIds}`,
    })
  },
  // 已收货
  getGoods(e) {
    let that = this
    let orderId = e.currentTarget.dataset.item.orderId
    let obj = {
      url: "/system/orders",
      method: "put",
      data: {
        "orderId": orderId, //1
        "status": 3,
      }
    }
    app.request.wxRequest(obj).then(res => {
      if (res.data.code == 200) {
        wx.showToast({
          title: '收货成功！',
          duration: 500,
          success: () => {
            that.setData({
              pageNum: 1,
              pageSize: 10,
              list: [],
            })
            that.getList()
          }
        })

      }
    })

  },
  payOrder(e) {
    let that = this
    wx.showModal({
      title: '提示',
      content: '微信支付',
      success(res) {
        if (res.confirm) {

          let orderId = e.currentTarget.dataset.item.orderId
          let obj = {
            url: "/system/orders",

            method: "put",
            data: {
              "orderId": orderId, //1
              "status": 2,
            }
          }
          app.request.wxRequest(obj).then(res => {
            if (res.data.code == 200) {
              wx.showToast({
                title: '支付成功！',
                duration: 500,
                success: () => {
                  that.setData({
                    pageNum: 1,
                    pageSize: 10,
                    list: [],
                  })
                  that.getList()
                }
              })

            }
          })

        } else if (res.cancel) {}
      }
    })
  },
  onChangeSearch(e) {
    this.setData({
      search: e.detail,
    });
  },
  onSearch() {

  },
  //顶部tab切换
  onChange(event) {
    let status = event.detail.name
    this.setData({
      list: [],
      pageNum: 1,
      pageSize: 10,
      active: status
    })
    this.getList()

  },
  getList() {
    let obj = {
      url: "/system/orders/list",
      method: "get",
      data: {
        "pageNum": this.data.pageNum,
        "pageSize": this.data.pageSize,
        "status": this.data.active,
        reasonable: false
      }
    }
    app.request.wxRequest(obj).then(res => {
      if (res.data.code == 200) {
        let list = res.data.rows
        list.forEach((item, i) => {
          let statusList = item.tOrdersItemsList.map(item => item.status)
          if (statusList.includes(0)) item.canComment = true
          if (statusList.includes(1)) item.canComment = false

          let pkIds = item.tOrdersItemsList.map(item => item.pkId)
          item.pkIds = pkIds
        })
        this.setData({
          list: [...this.data.list, ...list],
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    that.setData({
      "pageNum": 1,
      "pageSize": 10,
      list: []
    })
    that.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      pageNum: this.data.pageNum + 1
    })
    this.getList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})